$(function () {
    $("#jqGrid").jqGrid({
        url: '../sys/userTenant/list',
        datatype: "json",
        colModel: [
            {label: '租客姓名', name: 'userName', width: 75},
            {label: '租客电话', name: 'phone', width: 100},
            {label: '租客身份证号', name: 'idCard', width: 100},
            {label: '注册时间', name: 'createTime', index: "create_time", width: 80},
            {label: '操作', width: 80,formatter: function(value, options, row){
                return '<a class="layui-btn layui-btn-sm btn-detail" id="'+row.id+'">查看</a>'
                // return '<button class="layui-btn btn-detail" type="button" id="'+row.id+'"><i class="layui-icon">&#xe642;</i>查看</button>';
            }}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            $('.btn-detail').on('click', function () {
                var id = $(this).attr('id');
                vm.update(id)
            });
        }
    });
});

var organTree = "";
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    }
};

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            userName: null
        },
        showList: true,
        title: null,
        roleList: {},
        user: {
            status: 1,
            roleIdList: []
        },
        canEdit:true
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.canEdit = true;
            vm.showList = false;
            vm.title = "新增";
            vm.roleList = {};
            vm.user = {status: '0', createType: 'XIONG_AN', roleIdList: []};
            //获取角色信息
            this.getRoleList();
        },
        update: function (id) {
            vm.showList = false;
            vm.title = "查看";
            vm.canEdit = false;
            vm.getUser(id);
            //获取角色信息
            this.getRoleList();
        },
        del: function () {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../sys/userTenant/delete",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            toast(r, function (index) {
                                vm.reload();
                            });
                        } else {
                            alertMsg(r.msg);
                        }
                    }
                });
            });
        },
        enabled: function () {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            confirm('确定要启用选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../sys/userTenant/enabled",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            toast(r, function (index) {
                                vm.reload();
                            });
                        } else {
                            alertMsg(r.msg);
                        }
                    }
                });
            });
        },
        reset: function () {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            confirm('确定要重置选中的用户密码？', function () {
                $.ajax({
                    type: "POST",
                    url: "../sys/userTenant/reset",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            toast(r, function (index) {
                                vm.reload();
                            });
                        } else {
                            alertMsg(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function (event) {
            var url = vm.user.id == null ? "../sys/userTenant/save" : "../sys/userTenant/update";
            $.ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(vm.user),
                success: function (r) {
                    if (r.code == 0) {
                        if (vm.user.id == null) {
                            $layer.alert(r.msg,{
                                title: "提示",
                                shade: 0.6,
                                icon: 1,
                                closeBtn: 0,
                                btn: ["确定"],
                                yes: function(index, layero){
                                    vm.reload();
                                    $layer.close(index);
                                }
                            });
                            // toast(r, function (index, layer) {
                            //     vm.reload();
                            // }, function (index) {
                            //     vm.menu.sort = 1;
                            // })
                        } else {
                            toast(r.msg, function () {
                                vm.reload();
                            });
                        }

                    } else {
                        alertMsg(r.msg);
                    }
                }
            });
        },
        getUser: function (id) {
            $.get("../sys/userTenant/info/" + id, function (r) {
                vm.user = r.user;
            });
        },
        getRoleList: function () {
            $.get("../sys/role/select", function (r) {
                vm.roleList = r.list;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'phone': vm.q.phone, 'idCard': vm.q.idCard},
                page: page
            }).trigger("reloadGrid");
        }
    }
});